package edu.base.service;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.stat.TableStat.Name;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import edu.base.dao.CustomAdDao;
import edu.base.entity.Advance;
import edu.base.entity.Custom;
import edu.base.entity.Express;
import edu.base.entity.Wxzx;
import edu.base.model.Users;
import edu.base.util.HigherResponse;
/**
 * @author 92473
 *
 */
@Service
public class CustomAdServiceImpl implements CustomAdService{
	
	@Autowired
	private CustomAdDao customAdDao;
	
	//整合要加
	@Autowired
	UsersService usersService; 
	/*
	 * //客户登录
	 * 
	 * @Override public HigherResponse<Users> login(String username, String
	 * password, HttpSession session) {
	 * 
	 * Users queryLoginUser = customAdDao.queryCustom(username, password); if(null
	 * == queryLoginUser) { return HigherResponse.getFailedRespon("不存在这个账户"); } if
	 * (queryLoginUser.getRole() != 1) { return
	 * HigherResponse.getFailedRespon("请客户登录"); }
	 * 
	 * 
	 * //把信息传到session session.setAttribute("user",queryLoginUser); return
	 * HigherResponse.getSuccessRespon(queryLoginUser);
	 * 
	 * }
	 */
	
	//客户查询预约
	@Override
	public HigherResponse<PageInfo<Advance>> queryCustomAd(HttpSession session,Integer pageNum, Integer pageSize) {
		//整合要加，获取session
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));	
		Users custom = null;
		if (user instanceof Users) {
			custom = (Users) user;
		}
		if (null == custom) {
			return HigherResponse.getFailedRespon("未登录，请重新登录");
		}
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		int customId = custom.getUserId();
		List<Advance> queryAllAd = customAdDao.queryCustomAdvances(customId);
		if (null == queryAllAd) {
			return HigherResponse.getFailedRespon("您还没有预约");
		}
		PageInfo<Advance> pageInfo = new PageInfo<Advance>(queryAllAd);
		return HigherResponse.getSuccessRespon(pageInfo);
		
	}
	
	
	//客户添加预约信息(含维修设备故障图片)
	@Override
	public HigherResponse<String> addOneAdvance(HttpSession session, Advance advance, MultipartFile multipartFile) {
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));	
		Users custom = null;
		if (user instanceof Users) {
			custom = (Users) user;
		}
		if (null == custom) {
			return HigherResponse.getFailedRespon("未登录，请重新登录");
		}
		advance.setCustomId(custom.getUserId());	
		advance.setCustomPhone(custom.getTel());
		
		//判断文件是否是图片格式
		String originalFilename = multipartFile.getOriginalFilename();
		if (!originalFilename.endsWith(".jpg") && !originalFilename.endsWith(".png")) {
			return HigherResponse.getFailedRespon("图片格式不正确，请上传.jpg或者.png格式");
		}
		String[] imgs = originalFilename.split("\\.");
		//生成主图的名字
		UUID uid = UUID.randomUUID();
		String deviceImage = uid+"."+imgs[1];
		advance.setDeviceImage("/images/"+deviceImage);
		boolean addOneProduct = customAdDao.addOneAdvance(advance);
		if (addOneProduct==false) {
			return HigherResponse.getFailedRespon("添加失败，服务器未响应");
		}
		//添加商品信息成功后往虚拟目录添加图片 
		try {
			multipartFile.transferTo(new File("D:\\uploadimg\\"+deviceImage));
		} catch (IllegalStateException e) {
			return HigherResponse.getFailedRespon("上传图片失败");
		} catch (IOException e) {
			return HigherResponse.getFailedRespon("上传图片失败");
		}
		return HigherResponse.getSuccessRespon("上传图片成功");
		
	}
	
	//客户修改预约信息
	@Override
	public HigherResponse<String> updateAdvance(Integer advanceNo, Advance advance) {		
		
		int updateAdvance = customAdDao.updateAdvanceInfo(advance);
		if (0 ==updateAdvance) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");
	}
	
	
	//客户修改预约状态，取消预约
	@Override
	public HigherResponse<String> updateAdStatus(HttpSession session, Integer advanceId) {
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));	
		Users custom = null;
		if (user instanceof Users) {
			custom = (Users) user;
		}
		if (null == custom) {
			return HigherResponse.getFailedRespon("未登录，请重新登录");
		}
		boolean updateAdStatus = customAdDao.updateAdvanceStatus(custom.getUserId(), advanceId);
		if (false == updateAdStatus) {
			return HigherResponse.getFailedRespon("预约取消失败");
		}
		return HigherResponse.getSuccessRespon("取消预约成功");
	}
	
	//客户查询维修中心
	@Override
	public HigherResponse<List<Wxzx>> queryAllWxzx() {
		List<Wxzx> wxzxs = customAdDao.queryWxzx();
		
		return HigherResponse.getSuccessRespon(wxzxs);
	}
	
	
	//客户添加邮递
	@Override
	public HigherResponse<String> addOneExpress(HttpSession session,Integer advanceId, Express express) {
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));	
		Users custom = null;
		if (user instanceof Users) {
			custom = (Users) user;
		}
		if (null == custom) {
			return HigherResponse.getFailedRespon("未登录，请重新登录");
		}	
		express.setUserId(custom.getUserId());
		express.setAdvanceId(advanceId);
		boolean addOneExp = customAdDao.addOneExpress(express);
		if (false == addOneExp) {
			return HigherResponse.getFailedRespon("添加失败");
		}
		return HigherResponse.getSuccessRespon("添加成功");
	}
	
	
	//根据预约编号查询预约信息
	@Override
	public HigherResponse<Advance> queryOneAdByNo(Integer advanceNo) {
		Advance getOneAdByNo = customAdDao.queryOneAdvanceByAdNo(advanceNo);
		if (null == getOneAdByNo) {
			return HigherResponse.getFailedRespon("未查询到该预约信息");
		}
		return HigherResponse.getSuccessRespon(getOneAdByNo);
	}

	
	//客户查询邮递信息
	@Override
	public HigherResponse<Express> queryExpressInfo(HttpSession session,Integer advanceNo) {
		edu.base.model.Users user = usersService.findByKey((Integer) session.getAttribute("user_id"));	
		Users custom = null;
		if (user instanceof Users) {
			custom = (Users) user;
		}
		if (null == custom) {
			return HigherResponse.getFailedRespon("未登录，请重新登录");
		}
		
		
		Express queryExpress = customAdDao.queryOneExpress(custom.getUserId(), advanceNo);
		
		if (null == queryExpress ) {
			return HigherResponse.getFailedRespon("请添加邮递信息");
		}
		
		return HigherResponse.getSuccessRespon(queryExpress);
	}
	
	
	@Override
	public HigherResponse<Wxzx> queryWxzxByName(String wxzxName) {
		Wxzx wxzx = customAdDao.queryWxzxByName(wxzxName);
		if (null == wxzx ) {
			return HigherResponse.getFailedRespon("查询失败");
		}
		return HigherResponse.getSuccessRespon(wxzx);
	}

	
	//客服查询客户预约信息
	@Override
	public HigherResponse<PageInfo<Advance>> queryAllAdInfo(Integer pageNum, Integer pageSize, String beginTime,
			String endTime, Byte advanceMethod, Byte advanceStatus) {
		PageHelper.startPage(pageNum, pageSize);
		
		List<Advance> queryAllAd = customAdDao.queryAllAdInfo(beginTime, endTime, advanceMethod, advanceStatus);
		if (null == queryAllAd) {
			return HigherResponse.getFailedRespon("未查询到结果");
		}
		PageInfo<Advance> pageInfo = new PageInfo<Advance>(queryAllAd);
		return HigherResponse.getSuccessRespon(pageInfo);		
	}

	//客服查询客户的邮递信息
	@Override
	public HigherResponse<Express> getOneExpress(Integer advanceNo) {
        Express queryExpress = customAdDao.getOneExpress(advanceNo);		
		if (null == queryExpress ) {
			return HigherResponse.getFailedRespon("该用户还未添加邮递信息");
		}
		
		return HigherResponse.getSuccessRespon(queryExpress);
		
	}

	//客服删除客户的预约信息
	@Override
	public HigherResponse<String> deleteOneAdvance(Integer advanceNo) {
		
		boolean deleteOneAdvence = customAdDao.deleteAdvanceInfo(advanceNo);
		if (deleteOneAdvence == false) {
			return HigherResponse.getFailedRespon("删除失败");
		}
		return HigherResponse.getSuccessRespon("删除成功");
	}

	//客服确认预约
	@Override
	public HigherResponse<String> acceptAdvance(Integer advanceNo) {
		Advance custom = customAdDao.queryOneAdvanceByAdNo(advanceNo);
		if (custom == null) {
			return HigherResponse.getFailedRespon("预约已经取消"); 
		}
		if (custom.getAdvanceStatus() !=1 ) {
			return HigherResponse.getFailedRespon("该用户已经预约");
		}
		//修改客户的预约状态
		boolean updateStatus = customAdDao.acceptAdvance(advanceNo);
		if (false == updateStatus) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");
	}

	//客服查询邮递信息列表
	@Override
	public HigherResponse<PageInfo<Express>> queryAllExpress(Integer pageNum, Integer pageSize, Integer advanceId,
			Integer userId, String expressCompany) {
		PageHelper.startPage(pageNum, pageSize);
		
		List<Express> queryAllExpresses = customAdDao.getAllExpress(advanceId, userId, expressCompany);
		if (null == queryAllExpresses) {
			return HigherResponse.getFailedRespon("未查询到结果");
		}
		PageInfo<Express> pageInfo = new PageInfo<Express>(queryAllExpresses);
		return HigherResponse.getSuccessRespon(pageInfo);	
	}

	//客服查询维修中心信息
	@Override
	public HigherResponse<PageInfo<Wxzx>> queryWxzxList(Integer pageNum, Integer pageSize,Integer status) {
		PageHelper.startPage(pageNum, pageSize);
		
		List<Wxzx> queryAllWxzxs = customAdDao.getAllWxzxs(status);
		if (null == queryAllWxzxs) {
			return HigherResponse.getFailedRespon("未查询到结果");
		}
		PageInfo<Wxzx> pageInfo = new PageInfo<Wxzx>(queryAllWxzxs);
		return HigherResponse.getSuccessRespon(pageInfo);		
	}

	//客服修改维修中心状态
	@Override
	public HigherResponse<String> updateWxzxStatus(Integer wxzxId,Integer status) {
		boolean result = false;
		if (status == 1) {
			//先判断已经设置为正常使用			
			result = customAdDao.updateWxzxStatus(wxzxId, 2);		
		}else {
			result = customAdDao.updateWxzxStatus(wxzxId, 1);
		}
		if (result == false) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");		
	}

	//客服修改维修中心的管理者姓名
	@Override
	public HigherResponse<String> updateWxzxManager(Integer wxzxId, String wxzxManager) {
		boolean updateWxzxManager = customAdDao.updateWxzxManager(wxzxId, wxzxManager);
		if (false == updateWxzxManager) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		return HigherResponse.getSuccessRespon("修改成功");
	}

	//客服修改维修中心的管理者联系方式
	@Override
	public HigherResponse<String> updateWxzxPhone(Integer wxzxId, String phone) {
		boolean updateWxzxPhone = customAdDao.updateWxzxPhone(wxzxId, phone);
		if (false == updateWxzxPhone) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		
		return HigherResponse.getSuccessRespon("修改成功");
	}

	//客服修改维修中心的地址
	@Override
	public HigherResponse<String> updateWxzxAddress(Integer wxzxId, String wxzxAddress) {
		boolean updateWxzxAddress = customAdDao.updateWxzxAddress(wxzxId, wxzxAddress);
		if (false == updateWxzxAddress) {
			return HigherResponse.getFailedRespon("修改失败");
		}
		
		return HigherResponse.getSuccessRespon("修改成功");
	}
	
	//客服添加维修中心信息
	@Override
	public HigherResponse<String> addOneWxzx(Wxzx wxzx) {
		String nameString = wxzx.getWxzxName();
		Wxzx wxzx2 = customAdDao.queryWxzxByName(nameString);
		if (wxzx2 != null ) {
			return HigherResponse.getFailedRespon("该维修中心已存在");
		}
		boolean addOneWxzx =  customAdDao.addOneWxzx(wxzx);
		if (false == addOneWxzx) {
			return HigherResponse.getFailedRespon("添加失败");
		}
		return HigherResponse.getSuccessRespon("添加成功");
		
	}

	

	
	
	
	
			
	

	

}
